package com.umgsai.thread.thread50;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Service {
	private ReentrantLock lock = new ReentrantLock();
	private Condition condition = lock.newCondition();
	private boolean hasValue = false;
	public void set() {
		try {
			lock.lock();
			while (hasValue) {
				System.out.println("有可能★连续");
				condition.await();
			}
			System.out.println("★");
			hasValue = true;
			condition.signalAll();//通知在condition上等待的线程
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	public void get() {
		try {
			lock.lock();
			while (!hasValue) {
				System.out.println("有可能☆连续");
				condition.await();
			}
			System.out.println("☆");
			hasValue = false;
			condition.signalAll();//通知在condition上等待的线程
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
}
